package com.ninetaildemonfox.zdl.socialproject.mvp.ui.fragment.adapter;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ninetaildemonfox.zdl.ninetaildemonfox.base.BaseFragment;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.data.entity.SerializableList;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.PicturePresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.MainActivity;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.PicturePreviewActivity2;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * A simple {@link Fragment} subclass.
 */
public class PictureP2Fragment extends BaseFragment {

    @BindView(R.id.text_burn_after_reading)
    TextView textBurnAfterReading;
    @BindView(R.id.imageView)
    ImageView imageView;
    private Map<String, String> data;
    private String is_see;
    private Drawable drawable;
    @BindView(R.id.videoplayer)
    JZVideoPlayerStandard videoplayer;
    private PicturePresenter picturePresenter;
    public String TAG = "BlankFragment";
    public int count;
    private String cover_member_id;
    private int poation = 0;
    private int fenhui = 0;


    @SuppressLint("HandlerLeak")
    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_picture;
    }

    List<Map<String, String>> list;
    SerializableList serializableList;
    onTestListener onTestListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof onTestListener) {
            onTestListener = (onTestListener) activity; // 2.2 获取到宿主activity并赋值
        } else {
            throw new IllegalArgumentException("activity must implements FragmentInteraction");
        }
    }

    //把传递进来的activity对象释放掉
    @Override
    public void onDetach() {
        super.onDetach();
        onTestListener = null;
    }

    public interface onTestListener {
        void onTest(boolean xuanze);
    }

    @Override
    public void onInitView(Bundle savedInstanceState) {
        picturePresenter = new PicturePresenter(fgtContext);
        if (getArguments().getSerializable("serializableList") != null) {
            serializableList = (SerializableList) getArguments().getSerializable("serializableList");
            list = serializableList.getList();
        }
        list = PicturePreviewActivity2.list;
        cover_member_id = getArguments().getString("cover_member_id");
        poation = this.getArguments().getInt("poation");
        data = this.list.get(this.getArguments().getInt("poation"));
        //预览
        preview();
//        imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.d("onTest", "=======我进来了=======");
//                onTestListener.onTest(true);
//            }
//        });
    }


    private void preview() {
        textBurnAfterReading.setVisibility(View.GONE);
        //  是否设置阅后即焚  1否 2是
        if (TextUtils.isEmpty(cover_member_id)) {
            imageView.setVisibility(View.VISIBLE);
            Glide.with(me).load(data.get("pic")).into(imageView);
        } else {
            is_see = data.get("is_see");
            if (is_see.equals("2")) {
                //   是否看过图片  1是 2否
                if (data.get("have_see").equals("1")) {
                    imageView.setImageResource(R.mipmap.icon_burned);
                } else {
                    imageView.setImageResource(R.mipmap.icon_unburnt);
                    imageView.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            onTestListener.onTest(true);
                            if (data.get("have_see").equals("1")) {
                                imageView.setImageResource(R.mipmap.icon_burned);
                            } else {
                                // 是否是视频  0否 1是
                                if (data.get("is_video").equals("1")) {
                                    videoplayer.setVisibility(View.VISIBLE);
                                    imageView.setVisibility(View.GONE);
                                    //视频
                                    if (!TextUtils.isEmpty(data.get("url"))) {
                                        //视频路径
                                        videoplayer.setUp(data.get("url"),
                                                JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,
                                                "");
                                        if (data.get("pic") != null) {
                                            Bitmap bitmap1 = returnBitMap(data.get("pic"));
                                            //图片路径
                                            videoplayer.thumbImageView.setImageBitmap(bitmap1);
                                        }
                                    }
                                } else {
                                    //图片路径
                                    imageView.setVisibility(View.VISIBLE);
                                    videoplayer.setVisibility(View.GONE);
                                    Glide.with(me).load(data.get("pic")).into(imageView);
                                }
                                //图片id    被看人的id
                                picturePresenter.lookImage(token, data.get("id"), cover_member_id);
                            }
                            return true;
                        }
                    });
                    imageView.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            if (event.getAction() == MotionEvent.ACTION_UP) {
                                //抬起
                                onTestListener.onTest(false);
                                imageView.setVisibility(View.VISIBLE);
                                imageView.setImageResource(R.mipmap.icon_burned);
                                data.put("have_see", "1");
                            }
                            return false;
                        }
                    });
                }
            } else {
                // 是否是视频  0否 1是
                if (data.get("is_video").equals("1")) {
                    videoplayer.setVisibility(View.VISIBLE);
                    imageView.setVisibility(View.GONE);
                    //视频
                    if (!TextUtils.isEmpty(data.get("url"))) {
                        //视频路径
                        videoplayer.setUp(data.get("url"),
                                JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL,
                                "");
                        if (data.get("pic") != null) {
                            Bitmap bitmap1 = returnBitMap(data.get("pic"));
                            //图片路径
                            videoplayer.thumbImageView.setImageBitmap(bitmap1);
                        }
//                        videoplayer.startVideo();
                    }
                } else {
                    //图片路径
                    videoplayer.setVisibility(View.GONE);
                    imageView.setVisibility(View.VISIBLE);
                    Glide.with(me).load(data.get("pic")).into(imageView);
                }
            }
        }


    }


    public static PictureP2Fragment intentFragment(List<Map<String, String>> list, int poation, String cover_member_id) {
        PictureP2Fragment pictureP2Fragment = new PictureP2Fragment();
        Bundle bundle = new Bundle();
        SerializableList serializableList = new SerializableList(list);
        bundle.putSerializable("serializableList", serializableList);
        bundle.putInt("poation", poation);
        bundle.putString("cover_member_id", cover_member_id);
        pictureP2Fragment.setArguments(bundle);
        return pictureP2Fragment;
    }

    public static PictureP2Fragment intentFragment(int poation, String cover_member_id) {
        PictureP2Fragment pictureP2Fragment = new PictureP2Fragment();
        Bundle bundle = new Bundle();
        bundle.putInt("poation", poation);
        bundle.putString("cover_member_id", cover_member_id);
        pictureP2Fragment.setArguments(bundle);
        return pictureP2Fragment;
    }

    Bitmap bitmap;

    public Bitmap returnBitMap(final String url) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                URL imageurl = null;

                try {
                    imageurl = new URL(url);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                try {
                    if (imageurl == null){
                        return;
                    }
                    HttpURLConnection conn = (HttpURLConnection) imageurl.openConnection();
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    bitmap = BitmapFactory.decodeStream(is);
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return bitmap;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (videoplayer == null) {
            return;
        }
//        JZVideoPlayerStandard.releaseAllVideos();
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (videoplayer == null) {
            return;
        }
        JZVideoPlayer.releaseAllVideos();
//        JZVideoPlayerStandard.releaseAllVideos();
    }
}